﻿using System;
using System.Collections.Generic;
using Glue;
using Xunit;

namespace GlueExamples
{
    namespace MappingBetweenArrays
    {
        public class When_mapping_between_arrays_of_a_primitive_type
        {
            private class DomainPerson
            {
                public int[] Numbers { get; set; }
            }
            private class GuiPerson
            {
                public IList<int> Numbers { get; set; }
            }

            [Fact]
            public void Should_map_correctly()
            {
                var mapping = new Mapping<DomainPerson, GuiPerson>();
                mapping.Relate(domain => domain.Numbers, gui => gui.Numbers);
                var domainNumbers = new[] { 1, 2, 3 };
                var domainPerson = new DomainPerson { Numbers = domainNumbers };
                var guiPerson = mapping.Map(domainPerson, new GuiPerson());
                var guiNumbers = guiPerson.Numbers;

                Assert.Equal(domainNumbers.Length, guiNumbers.Count);
                Assert.Equal(domainNumbers[0], guiNumbers[0]);
                Assert.Equal(domainNumbers[1], guiNumbers[1]);
                Assert.Equal(domainNumbers[2], guiNumbers[2]);
            }
        }

        public class When_mapping_between_arrays_of_a_nested_type
        {
            private class DomainPerson { public DomainAddress[] Addresses { get; set; } }
            private class DomainAddress { public String City { get; set; } }

            private class GuiPerson { public GuiAddress[] Addresses { get; set; } }
            private class GuiAddress { public String City { get; set; } }

            [Fact]
            public void Should_map_correctly()
            {
                var addressMapping = new Mapping<DomainAddress, GuiAddress>(x => new DomainAddress(), x => new GuiAddress());
                addressMapping.AutoRelateEqualNames();

                var mapping = new Mapping<DomainPerson, GuiPerson>();
                mapping.Relate(domain => domain.Addresses, gui => gui.Addresses, addressMapping);

                var domainPerson = new DomainPerson
                {
                    Addresses = new DomainAddress[]
                           {
                               new DomainAddress {City = "City1"}, 
                               new DomainAddress {City = "City2"}
                           }
                };

                var guiPerson = mapping.Map(domainPerson, new GuiPerson());
                Assert.Equal(domainPerson.Addresses.Length, guiPerson.Addresses.Length);
                Assert.Equal(domainPerson.Addresses[0].City, guiPerson.Addresses[0].City);
                Assert.Equal(domainPerson.Addresses[1].City, guiPerson.Addresses[1].City);
            }
        }
    }
}
